package com.myspringboot.workerpool.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by guohuixin on 2017/12/14.
 */
@Configuration
public class ThreadPoolConfig {
    @Value("${thread.corePoolSize}")
    private int corePoolSize;

    @Value("${thread.queueCapacity}")
    private int queueCapacity;

    @Value("${thread.maxPoolSize}")
    private int maxPoolSize;

    @Value("${thread.KeepAliveTime}")
    private int keepAliveTime;

    @Bean(name = "com.mysrpingboot.taskPool")
    public Executor taskPool(){
        ThreadPoolTaskExecutor  executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setKeepAliveSeconds(keepAliveTime);
        executor.setQueueCapacity(queueCapacity);
        executor.setMaxPoolSize(maxPoolSize);

        //超出队列在过来的任务处理方式
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }
}
